from toolbox import BaiduMap
from config import * 
baidu_api = BaiduMap(BD_AK)
import pymysql


def insert_station_info():
    db = pymysql.connect(host=HOST, user=USER, passwd=PASSWORD, db=DB, charset=CHARSET)
    cursor = db.cursor()
    # 查找数据库，如果station重复则保留一个结果
    station_number = cursor.execute('select * from data group by station having count(station)>1')
    # 所有查询结果
    station_result = cursor.fetchall()
    # 遍历所有结果，然后把数据插入info表，其中字段station_id name latitude longitude从原始表data取值不修改
    # 字段country province city district分别代表国家，省份，城市，县或区级信息是通过传递经纬度调用百度api取到的
    for i in station_result:  
        #print(baidu_api.get_location(i[2],i[3]))
        sql = 'insert into info values(%s, %s, %s, %s, %s, %s, %s, %s)'
        values = [i[0],i[5],i[2],i[3],baidu_api.get_location(i[2],i[3])['country'],baidu_api.get_location(i[2],i[3])['province'],baidu_api.get_location(i[2],i[3])['city'],baidu_api.get_location(i[2],i[3])['district']]
        print(values)
        cursor.execute(sql,values)
        db.commit()